package com.zfsn.okaria.logger;

import com.blankj.utilcode.util.Utils;
import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.flattener.Flattener;
import com.elvishew.xlog.flattener.Flattener2;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.file.FilePrinter;
import com.elvishew.xlog.printer.file.backup.FileSizeBackupStrategy2;
import com.elvishew.xlog.printer.file.clean.FileLastModifiedCleanStrategy;
import com.elvishew.xlog.printer.file.naming.FileNameGenerator;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class XLogManager {
    public static final String TAG = "XLogManager";
    public static final String TAG_INIT = "TAG_INIT";
    public static final String TAG_HTTP = "TAG_HTTP";
    public static XLogManager getInstance() {
        return XLogManagerHolder.INSTANCE;
    }
    private static class XLogManagerHolder{
        private static final XLogManager INSTANCE = new XLogManager();
    }
    public  String getLogPathFileLastFromLogLevel( int logLevel){
        String logPath = getLogPath();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String targetFilepath = logPath + "/" + LogLevel.getLevelName(logLevel) + "-" + "myLog" + "-" + sdf.format(new Date()) + ".log";
        System.out.println("getLogPathFileLastFromLogLevel:"+targetFilepath);
        return targetFilepath;
    }
    public  String  getLogPath(){
        String logPath = Utils.getApp().getExternalCacheDir().getAbsolutePath()+"/log";
        File file = new File(logPath);
        boolean fileResult = file.exists() ? file.isDirectory() : file.mkdirs();
        return logPath;
    }
    public void init(){
        LogConfiguration config = (new LogConfiguration.Builder())
                .logLevel(LogLevel.ALL)
                .tag("okAria")            // Specify TAG, default: "X-LOG" 指定标签，默认:“X-LOG”
//                .enableStackTrace(1)   // Enable stack trace info with depth 2, disabled by default 启用深度为2的堆栈跟踪信息，默认禁用
//                .enableBorder()        // Enable border, disabled by default 启用边框，默认禁用
                .build();
        FilePrinter filePrinter2 =new FilePrinter  // Printer that print(save) the log to file 打印(保存)日志到文件的打印机
                .Builder(getLogPath())// Specify the directory path of log file(s) 指定日志文件的目录路径
                .fileNameGenerator(new MyFileNameGenerator()) //自定义文件名称 默认值:ChangelessFileNameGenerator(“日志”)
                .backupStrategy(new FileSizeBackupStrategy2(10*3 * 1024 * 1024,3 * 1024 * 1024)) //单个日志文件的大小默认:FileSizeBackupStrategy(1024 * 1024)
                .cleanStrategy( new FileLastModifiedCleanStrategy(  7L * 24L * 60L * 60L * 1000L))  //日志文件存活时间，单位毫秒
                .flattener(new MyFlattener()) //自定义flattener，控制打印格式
                .build();
        XLog.init(config, new AndroidPrinter(true), filePrinter2);
    }
    class MyFlattener  implements Flattener, Flattener2 {

        @Override
        public CharSequence flatten(int logLevel, String tag, String message) {
            return flatten(System.currentTimeMillis(), logLevel, tag, message);
        }
        @Override
        public CharSequence flatten(long timeMillis, int logLevel, String tag, String message) {
            return (getCurrDDate()
                    + '|' + LogLevel.getLevelName(logLevel)
                    + '|' + tag
                    + '|' + message);
        }
        String getCurrDDate() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
        }
    }
    class MyFileNameGenerator implements FileNameGenerator {
        @Override
        public boolean isFileNameChangeable() {
            return true;
        }

        @Override
        public String generateFileName(int logLevel, long timestamp) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return LogLevel.getLevelName(logLevel) + "-" + "myLog" + "-" + sdf.format(new Date(timestamp)) + ".log";
        }
    }
}
